package controller

import (
	"server/internal/app/dto"
	"server/internal/app/service"
	"server/internal/core/server"

	"github.com/gin-gonic/gin"
)

type RbacController struct {
	server.Controller
	adminRbacService *service.AdminRbacService
}

func NewRbacController() *RbacController {
	return &RbacController{
		adminRbacService: service.NewAdminRbacService(),
	}
}

// 角色管理 ================================
func (c *RbacController) GetRoleList(ctx *gin.Context) {
	roles, err := c.adminRbacService.GetRoleList(ctx)
	if err != nil {
		c.Error(ctx, err)
		return
	}
	c.Success(ctx, roles)
}

func (c *RbacController) CreateRole(ctx *gin.Context) {
	request := &dto.CreateRoleRequest{}
	if err := ctx.ShouldBindJSON(request); err != nil {
		c.Error(ctx, err)
		return
	}
	err := c.adminRbacService.CreateRole(ctx, request)
	if err != nil {
		c.Error(ctx, err)
		return
	}
	c.Success(ctx, nil)
}

func (c *RbacController) UpdateRole(ctx *gin.Context) {
	request := &dto.UpdateRoleRequest{}
	if err := ctx.ShouldBindJSON(request); err != nil {
		c.Error(ctx, err)
		return
	}
	err := c.adminRbacService.UpdateRole(ctx, request)
	if err != nil {
		c.Error(ctx, err)
		return
	}
	c.Success(ctx, nil)
}

func (c *RbacController) DeleteRole(ctx *gin.Context) {
	request := &dto.DeleteRoleRequest{}
	if err := ctx.ShouldBindJSON(request); err != nil {
		c.Error(ctx, err)
		return
	}
	err := c.adminRbacService.DeleteRole(ctx, request)
	if err != nil {
		c.Error(ctx, err)
		return
	}
	c.Success(ctx, nil)
}

// 权限管理 ================================
func (c *RbacController) GetPermissionList(ctx *gin.Context) {
	permissions, err := c.adminRbacService.GetPermissionList(ctx)
	if err != nil {
		c.Error(ctx, err)
		return
	}
	c.Success(ctx, permissions)
}

func (c *RbacController) CreatePermission(ctx *gin.Context) {
	request := &dto.CreatePermissionRequest{}
	if err := ctx.ShouldBindJSON(request); err != nil {
		c.Error(ctx, err)
		return
	}
	err := c.adminRbacService.CreatePermission(ctx, request)
	if err != nil {
		c.Error(ctx, err)
		return
	}
	c.Success(ctx, nil)
}

func (c *RbacController) UpdatePermission(ctx *gin.Context) {
	request := &dto.UpdatePermissionRequest{}
	if err := ctx.ShouldBindJSON(request); err != nil {
		c.Error(ctx, err)
		return
	}
	err := c.adminRbacService.UpdatePermission(ctx, request)
	if err != nil {
		c.Error(ctx, err)
		return
	}
	c.Success(ctx, nil)
}

func (c *RbacController) DeletePermission(ctx *gin.Context) {
	request := &dto.DeletePermissionRequest{}
	if err := ctx.ShouldBindJSON(request); err != nil {
		c.Error(ctx, err)
		return
	}
	err := c.adminRbacService.DeletePermission(ctx, request)
	if err != nil {
		c.Error(ctx, err)
		return
	}
	c.Success(ctx, nil)
}

// 分配权限 ================================
func (c *RbacController) AssignRolePermission(ctx *gin.Context) {
	request := &dto.AssignRolePermissionRequest{}
	if err := ctx.ShouldBindJSON(request); err != nil {
		c.Error(ctx, err)
		return
	}
	err := c.adminRbacService.AssignRolePermission(ctx, request)
	if err != nil {
		c.Error(ctx, err)
		return
	}
	c.Success(ctx, nil)
}

// 分配管理员角色 ================================
func (c *RbacController) AssignAdminRole(ctx *gin.Context) {
	request := &dto.AssignAdminRoleRequest{}
	if err := ctx.ShouldBindJSON(request); err != nil {
		c.Error(ctx, err)
		return
	}
	err := c.adminRbacService.AssignAdminRole(ctx, request)
	if err != nil {
		c.Error(ctx, err)
		return
	}
	c.Success(ctx, nil)
}
